package com.buka.communicate.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.buka.communicate.R;
import com.buka.communicate.adapter.GiftNumAdapter;
import com.buka.communicate.fragment.GiftFuBowFragment;
import com.buka.communicate.test.TabFragment;

import net.neiquan.applibrary.base.BaseActivity;
import net.neiquan.applibrary.base.FragmentAdapter;
import net.neiquan.applibrary.base.MyBaseAdapter;
import net.neiquan.applibrary.wight.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/20 0020.
 */

public class GiftActivity extends BaseActivity {
    @InjectView(R.id.tabLayout)
    TabLayout mTabLayout;
    @InjectView(R.id.viewPager)
    ViewPager mViewPager;
    @InjectView(R.id.remainLogo)
    ImageView remainLogo;
    @InjectView(R.id.remainMoney)
    TextView remainMoney;
    @InjectView(R.id.addNum)
    ImageView addNum;
    @InjectView(R.id.sendNum)
    TextView sendNum;
    @InjectView(R.id.sendGift)
    TextView sendGift;
    @InjectView(R.id.mGiftLL)
    LinearLayout mGiftLL;
    private String[] titles = new String[]{"One", "Two", "Three"};

    @Override
    public int getRootViewId() {
        return R.layout.activity_gift;
    }

    @Override
    public void setViews() {
        setTitleTv("礼物");
        setNextTv("礼物说明");
        setupViewPager();
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabSelect(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabNormal(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.addNum, R.id.sendNum, R.id.mGiftLL})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.addNum:
                break;
            case R.id.mGiftLL:
                showGiftNumPopWindow();
                break;
            case R.id.sendGift:
                break;
        }
    }

    private void showGiftNumPopWindow() {
        CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(this)
                //设置PopupWindow布局
                .setView(R.layout.gift_num_pop)
                //设置宽高
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置动画
                .setAnimationStyle(R.style.Animation_right)
                //设置背景颜色，取值范围0.0f-1.0f 值越小越暗 1.0f为透明
                .setBackGroundLevel(1.0f)
                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(final PopupWindow popupWindow, View view, int layoutResId) {
                        RecyclerView mRecylerView = (RecyclerView) view.findViewById(R.id.mRecylerView);
                        initRecylerView(mRecylerView);
                        final GiftNumAdapter oderPopAdapter = new GiftNumAdapter(GiftActivity.this, null);
                        mRecylerView.setAdapter(oderPopAdapter);
                        oderPopAdapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
                            @Override
                            public void onItemClick(View view, List data, int position) {
                                popupWindow.dismiss();
                            }
                        });
                    }
                })
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();
        //弹出PopupWindow
//        popupWindow.showAsDropDown(mGiftLL,0, 10);
        int[] location = new int[2];
        mGiftLL.getLocationOnScreen(location);
        popupWindow.showAtLocation(mGiftLL, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight()-40);
    }

    private void initRecylerView(RecyclerView mRecylerView) {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,
                StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(StaggeredGridLayoutManager.VERTICAL);
        mRecylerView.setHasFixedSize(true);
        mRecylerView.setLayoutManager(layoutManager);
    }


    public ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new GiftFuBowFragment());
        fragments.add(new GiftFuBowFragment());
        fragments.add(new GiftFuBowFragment());
        return fragments;
    }

    private int[] tabIcons = {
            R.drawable.qq,
            R.drawable.qq,
            R.drawable.qq,
    };

    private int[] tabIconsPressed = {
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher,
    };


    private void setupViewPager() {
        ArrayList<Fragment> fragments = getFragments();
        if (titles != null && titles.length > 0 && fragments != null && fragments.size() > 0 && titles.length == fragments.size()) {
//            for (int i = 0; i < title.length; i++) {
//                mTabLayout.addTab(mTabLayout.newTab().setText(title[i]));
//            }
            FragmentAdapter adapter =
                    new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
            mViewPager.setAdapter(adapter);
            mTabLayout.setupWithViewPager(mViewPager);
            mTabLayout.setTabsFromPagerAdapter(adapter);
            for (int i = 0; i < titles.length; i++) {
                mTabLayout.getTabAt(i).setCustomView(getTabView(i, titles));
            }
        }
    }

    public View getTabView(int position, String[] titles) {
        //这是头部导航的布局
        View view = LayoutInflater.from(this).inflate(net.neiquan.applibrary.R.layout.item_tab, null);
        TextView txt_title = (TextView) view.findViewById(net.neiquan.applibrary.R.id.txt_title);
        txt_title.setText(titles[position]);
        ImageView img_title = (ImageView) view.findViewById(net.neiquan.applibrary.R.id.img_title);
        img_title.setImageResource(tabIcons[position]);
        if (position == 0) {
            //这个地方设置初始字体选中颜色
            txt_title.setTextColor(Color.RED);
            img_title.setImageResource(tabIconsPressed[position]);
        } else {
            //这个地方设置初始字体未选中颜色
            txt_title.setTextColor(Color.BLACK);
            img_title.setImageResource(tabIcons[position]);
        }
        return view;
    }

    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(net.neiquan.applibrary.R.id.img_title);
        TextView txt_title = (TextView) view.findViewById(net.neiquan.applibrary.R.id.txt_title);
        //这个地方设置字体选中颜色
        txt_title.setTextColor(Color.RED);
        if (titles != null && titles.length > 0 && tabIconsPressed != null && tabIconsPressed.length > 0 && titles.length == tabIconsPressed.length) {
            img_title.setImageResource(tabIconsPressed[tab.getPosition()]);
        }
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        ImageView img_title = (ImageView) view.findViewById(net.neiquan.applibrary.R.id.img_title);
        TextView txt_title = (TextView) view.findViewById(net.neiquan.applibrary.R.id.txt_title);
        //这个地方设置字体未选中颜色
        txt_title.setTextColor(Color.BLACK);
        if (titles != null && titles.length > 0 && tabIcons != null && tabIcons.length > 0 && titles.length == tabIcons.length) {
            img_title.setImageResource(tabIcons[tab.getPosition()]);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
